package com.example.dllo.baidumusic.main;
/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         佛祖保佑       永无BUG
*/

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import com.example.dllo.baidumusic.R;
import com.example.dllo.baidumusic.adapter.PlayIngAdapter;
import com.example.dllo.baidumusic.bean.MusicBean;
import com.example.dllo.baidumusic.bean.MusicBeanListBean;
import com.example.dllo.baidumusic.events.AskState;
import com.example.dllo.baidumusic.events.NextEvent;
import com.example.dllo.baidumusic.events.PreviousEvent;
import com.example.dllo.baidumusic.events.SingInfoEvent;
import com.example.dllo.baidumusic.fragment.LrcFragment;
import com.example.dllo.baidumusic.fragment.PicFragment;
import com.example.dllo.baidumusic.nettool.BaseActivity;
import com.example.dllo.baidumusic.service.PlayService;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/8/23.
 */
public class PlayMusicAcyivity extends BaseActivity {
    private TextView sing;
    private TextView singer;
    private ViewPager viewPager;
    private ImageButton imageButton;
    private SeekBar seekBar;
    private ImageButton stop;
    private ImageButton next;
    private ImageButton shang;
    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            musicBinder = (PlayService.MusicBinder) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };
    private PlayService.MusicBinder musicBinder;



    @Override
    protected int setLayout() {
        return R.layout.activity_playmusic;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        imageButton = bindView(R.id.activity_playmusic_fanhui);
        imageButton.setOnClickListener(this);
        seekBar = bindView(R.id.activity_playmusic_seekbar);
        stop = bindView(R.id.palying_stop);
        next = bindView(R.id.palying_next);
        shang = bindView(R.id.palying_shang);
        stop.setOnClickListener(this);
        next.setOnClickListener(this);
        shang.setOnClickListener(this);
        sing = bindView(R.id.activity_playmusic_singer);
        singer = bindView(R.id.activity_playmusic_singname);
        sing.setHorizontallyScrolling(true);
        sing.setFocusable(true);
        singer.setHorizontallyScrolling(true);
        singer.setFocusable(true);
        viewPager = bindView(R.id.activity_playmusic_viewpager);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser){
                    musicBinder.seekTo(progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SingInfoEvent singInfoEvent =new SingInfoEvent();
        EventBus.getDefault().post(singInfoEvent); //发送要数据的请求

        Intent playServiceIntent = new Intent(this,PlayService.class);
        bindService(playServiceIntent,conn, BIND_AUTO_CREATE);
        IntentFilter filter = new IntentFilter();
        filter.addAction(PlayService.ACTION_PROGRESS);
        registerReceiver(mReceiver,filter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getMusicInfo(MusicBean musicBean){
        singer.setText(musicBean.getMusicName());
        sing.setText(musicBean.getSingerName());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void  getMusicDeatil2(MusicBeanListBean musicBeanListBean){
        List<MusicBean> musicBeanList =musicBeanListBean.getMusicBeen();
        int position = musicBeanListBean.getPosition();
        sing.setText(musicBeanList.get(position).getMusicName());
        singer.setText(musicBeanList.get(position).getSingerName());

    }

    @Override
    protected void initData() {
        ArrayList<Fragment> arrayList = new ArrayList<>();
        arrayList.add(new PicFragment());
        arrayList.add(new LrcFragment());
        PlayIngAdapter fragmentPager =
                new PlayIngAdapter(getSupportFragmentManager(), arrayList);
        viewPager.setAdapter(fragmentPager);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_playmusic_fanhui:
                finish();
                break;
            case R.id.palying_stop:
                AskState askState = new AskState();
                EventBus.getDefault().post(askState);
                break;
            case R.id.palying_next:
                NextEvent nextEvent = new NextEvent();
                EventBus.getDefault().post(nextEvent);
                break;
            case R.id.palying_shang:
                PreviousEvent previousEvent = new PreviousEvent();
                EventBus.getDefault().post(previousEvent);
                break;
        }

    }
    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action){
                case PlayService.ACTION_PROGRESS:
                    int progress = intent.getIntExtra(PlayService.KEY_PRORESS, 0);
                    seekBar.setProgress(progress);
                    break;
            }
        }
    };
}
